package cn.com.generaldata.os.service.indicator_evaluation.monitoring;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.random.R;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.pojo.IndicatorEvaluationResult;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 二期预警指数评估指标计算类-针对曲线图数据计算
 * 2017-6-12
 */
public class SecondPhasendIcatorEarlyWarningBightCalculateService extends IndicatorEvaluationCalculateService {

	private final List<String> comprehensiveName = ProvinceIndicatorConstant.WARNING_INDICATOR_NAMES;
	

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double sum = 0d;//年度9个指标的得分*指标权重
		for (String name : comprehensiveName) {
		 
			//根据指标名称从tbl_indicator_evaluation表获取指标id
			Integer warningIndicatorId = getIndicatorEvaluationId(name, indicatorAccountVO.getAreaId());
			//tbl_indicator_evaluation_res 获取指标对应的信号灯值(1-5)
			Double warningIndicatorVal = getWarningIndicatorVal(indicatorAccountVO, warningIndicatorId);
			//获取指标平均权重
			Double weight = getIndicatorWeight(name);
			sum +=warningIndicatorVal*weight;
		}
		 
		//获取总指标年度综合预警指数
		return sum*comprehensiveName.size();
	}

	 
	/**
	 * 获取指标的过冷、偏冷、正常、偏热、过热的值
	 * 2017-6-12
	 * int id 指标id
	 * */
	private Double getWarningIndicatorVal(IndicatorAccountVO indicatorAccountVO, int id) {
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), id);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}
	/**
	 * 根据指标名称，获取指标平均权重
	 * 2017-6-13
	 * */
	private Double getIndicatorWeight(String iname){
		Double result = null;
		SqlTemplate sqlTemplate = new SqlTemplate(dao);
		if(StringUtils.isNotBlank(iname)){
			String sql = "select weight from T_INDICATOR_EVALUATION where name="+iname;
			result = sqlTemplate.queryForObject(sql, null, Double.class);
		}
		return result;
		
	}

}
